<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/28
 * Time: 13:38
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\DecorationService;
use App\Constant\ErrorConstant;

class DecorationController extends BaseController
{
    protected $service;

    public function __construct(Request $request, DecorationService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 基础页面列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function basicTemplate()
    {
        $page   =   (int) $this->request->input('page', 1);
        $type   =   (int) $this->request->input('type', 0);
        $size   =   (int) $this->request->input('page_size', 20);
        [$flag, $result]    =   $this->service->templateBasic($type, $page, $size);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_LIST_ERROR, $result);
        }
    }

    /**
     * 专题页面列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function specialTemplate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query' =>  'array'
        ], [
            'query.array'   =>  '搜索条件必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_LIST_ERROR, $validator->errors()->first());
        }

        $query  =   $this->request->input('query', []);
        $page   =   (int) $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->templateSpecial($query, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_LIST_ERROR, $result);
        }
    }

    /**
     * 修改页面标题
     * @return \Illuminate\Http\JsonResponse
     */
    public function titleTemplate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'template_id'   =>  'required|string|exists:mongodb.shopTemplate,_id',
            'template_name' =>  'required|string|max:50'
        ], [
            'template_id.required'  =>  '模板ID不能为空',
            'template_id.string'    =>  '模板ID必须为字符',
            'template_id.exists'    =>  '模板ID不存在',

            'template_name.required'=>  '页面标题不能为空',
            'template_name.string'  =>  '页面标题必须为字符',
            'template_name.max'     =>  '页面标题过长'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_TITLE_ERROR, $validator->errors()->first());
        }

        $template_id    =   $this->request->post('template_id');
        $template_name  =   $this->request->post('template_name');

        [$flag, $result]=   $this->service->templateTitle($template_id, $template_name);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_TITLE_ERROR, $result);
        }
    }

    /**
     * 查看页面标题
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoTemplate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'template_id'   =>  'required|string',
        ], [
            'template_id.required'  =>  '模板ID不能为空',
            'template_id.string'    =>  '模板ID必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_INFO_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->templateInfo($this->request->post('template_id'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_INFO_ERROR, $result);
        }
    }

    /**
     * 文件图片上传
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadTemplate()
    {
        $validator = Validator::make($this->request->all(), [
            'file' => 'required|image|max:5120'
        ], [
            'file.required' =>  '上传文件不能为空',
            'file.image'    =>  '上传文件必须为图片',
            'file.max'      =>  '上传文件过大'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_UPLOAD_ERROR, $validator->errors()->first());
        }

        $file   =   $this->request->all();
        [$flag, $result] = $this->service->templateUpload($file['file']);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_UPLOAD_ERROR, $result);
        }
    }

    /**
     * 视频上传
     * @return \Illuminate\Http\JsonResponse
     */
    public function videoTemplate()
    {
        $validator = Validator::make($this->request->all(), [
            'file' => 'required|mimetypes:video/avi,video/mpeg,video/quicktime|max:102400'
        ], [
            'file.required' =>  '上传视频不能为空',
            'file.mimetypes'=>  '上传视频格式',
            'file.max'      =>  '上传文件过大'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_UPLOAD_ERROR, $validator->errors()->first());
        }

        $file   =   $this->request->all();
        [$flag, $result] = $this->service->templateUpload($file['file']);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_UPLOAD_ERROR, $result);
        }
    }

    /**
     * 预览设置
     * @return \Illuminate\Http\JsonResponse
     */
    public function previewTemplate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'template_id'   =>  'required|string|exists:mongodb.shopTemplate,_id',
            'template_setting'  =>  'required',
        ], [
            'template_id.required'  =>  '模板ID不能为空',
            'template_id.string'    =>  '模板ID必须为字符',
            'template_id.exists'    =>  '模板不存在',

            'template_setting.required' =>  '模板设置不能为空',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_PREVIEW_ERROR, $validator->errors()->first());
        }

        $template_id        =   $this->request->post('template_id');
        $template_setting   =   json_decode($this->request->post('template_setting'), true);
        if (empty($template_setting)) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_PREVIEW_ERROR, '设置不能为空');
        }

        [$flag, $result]    =   $this->service->templatePreview($template_setting, $template_id);
        if ($flag) {
            return $this->successResponse();
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_PREVIEW_ERROR, $result);
        }
    }

    /**
     * 保存并发布
     * @return \Illuminate\Http\JsonResponse
     */
    public function releaseTemplate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'template_id'   =>  'required|string|exists:mongodb.shopTemplate,_id'
        ], [
            'template_id.required'  =>  '模板ID不能为空',
            'template_id.string'    =>  '模板ID必须为字符',
            'template_id.exists'    =>  '模板不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_RELEASE_ERROR, $validator->errors()->first());
        }

        $template_id    =   $this->request->post('template_id');
        [$flag, $result]    =   $this->service->templateRelease($template_id);
        if ($flag) {
            return $this->successResponse();
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_RELEASE_ERROR, $result);
        }
    }

    /**
     * 装修二维码预览
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\StreamedResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function codeTemplate()
    {
        $validator  =   Validator::make($this->request->post(), [
            'template_id'   =>  'required|string|exists:mongodb.shopTemplate,_id'
        ], [
            'template_id.required'  =>  '模板ID不能为空',
            'template_id.string'    =>  '模板ID必须为字符',
            'template_id.exists'    =>  '模板不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_CODE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->templateCode($this->request->post('template_id'));
        if ($flag) {
            return response()->stream(function () use ($result) {
                echo $result;
            }, 200, ['Content-Type' => 'image/jpeg']);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_DECORATION_CODE_ERROR, $result);
        }
    }
}
